
#import "JYTabBar.h"

@interface JYTabBar ()
@property (nonatomic, strong) NSMutableArray *tabBarButtons;

@end

@implementation JYTabBar
@synthesize pointMuDic;
/**
 *  数组的懒加载
 */
- (NSMutableArray *)tabBarButtons
{
    if (!_tabBarButtons) {
        self.tabBarButtons = [NSMutableArray array];
    }
    return _tabBarButtons;
}

- (id)initWithFrame:(CGRect)frame
{
    DLog(@"int with frame");

    if (self = [super initWithFrame:frame]) {
        // 设置背景
      //  self.backgroundColor = JYColor(82, 82, 82);
        self.backgroundColor = [UIColor whiteColor];
        CALayer *layer1 = [[CALayer alloc]init];
        layer1.backgroundColor =  [JYColor(246, 246, 246)CGColor];
        layer1.frame = CGRectMake(0, 0, mainScreenWidth, 2);
        [self.layer addSublayer:layer1];     //在view 上画个线条
        
        pointMuDic = [NSMutableDictionary dictionaryWithCapacity:1];
    }
    return self;
}
/**
 *  添加一个选项卡按钮
 */
- (void)addTabBarButton:(UITabBarItem *)item
{
  //liurg  if ([item.title isEqualToString:@"首页"]) return;
    // 创建按钮
    JYTabBarButton *button = [[JYTabBarButton alloc] init];
    button.item = item;
    [button addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchDown];
    NSInteger tag = self.tabBarButtons.count;
   /*liurg if (tag >= 2) {
        tag += 1;
    } */
//    if (tag== 0) {
//        
//    }
    
    //  如果tag ＝0的话，是对象本身自己， 所以tag值在subView中不允许赋值为0
    button.tag = tag+100;
    DLog(@"tag = %d",tag);
    [self addSubview:button];
    
    // 加到数组中
    [self.tabBarButtons addObject:button];
    // 默认让最前面的按钮选中
    if (self.tabBarButtons.count == 1) {
        [self tabBarButtonClick:button];
    }
}
/**
 *  点击选项卡按钮
 */
- (void)tabBarButtonClick:(JYTabBarButton *)button
{
    // 通知代理
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectButtonFrom:to:)]) {
        long from = self.selectedTabBarButton.tag;
        long to = button.tag;
        [self.delegate tabBar:self didSelectButtonFrom:from to:to];
    }
    // 更改按钮状态
    self.selectedTabBarButton.enabled = YES;
    button.enabled = NO;
    self.selectedTabBarButton = button;
}
/**
 *  点击了首页按钮
 */
- (void)selectedHome
{
    self.selectedTabBarButton.enabled = YES;
    self.selectedTabBarButton = nil;
}

/**
 *  布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    DLog(@"layoutSubviews");
    // 1.设置选项卡按钮的位置和尺寸
    [self setupTabBarButtonFrame];
}

/**
 *  设置选项卡按钮的位置和尺寸
 */
- (void)setupTabBarButtonFrame
{
    NSUInteger count = self.tabBarButtons.count;
   //liurg CGFloat buttonW = self.width / (count + 1);
    CGFloat buttonW = self.width / count;
    CGFloat buttonH = self.height;
    for (int i = 0; i < count; i++) {
        JYTabBarButton *button = self.tabBarButtons[i];
        button.width = buttonW;
        button.height = buttonH;
        button.x = buttonW * i;
        button.y = 0;
        newPoint[i] = [[UIView alloc]initWithFrame:CGRectMake(button.frame.size.width - 18, 8, 8, 8)];
        [newPoint[i] setBackgroundColor:[UIColor redColor]];
        [newPoint[i].layer setCornerRadius:4.0];
        [newPoint[i] setHidden:YES];
        [button addSubview:newPoint[i]];
        NSString *pointStr = [NSString stringWithFormat:@"point%d",i];
        [pointMuDic setObject:newPoint[i] forKey:pointStr];
// liurg       if (i >= count / 2) {
//            button.x += buttonW;
//        }
    }
        
}

@end
